﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace _2D
{
    public class CollisionEngine
    {
        // Lista de objetos colisionables pasivos.
        private Rectangle[] _collisionBoxes;
        public Rectangle[] collisionBoxes
        {
            get
            {
                return _collisionBoxes;
            }
            set
            {
                _collisionBoxes = value;
            }
        }

        private ICollisionable[] _collisionableObjects;
        public ICollisionable[] collisionableObjects
        {
            get
            {
                return _collisionableObjects;
            }
            set
            {
                _collisionableObjects = value;
            }
        }

        /// <summary>
        /// Inicializamos las colecciones de objetos.
        /// </summary>
        public CollisionEngine()
        {
            _collisionBoxes = new Rectangle[0];
            _collisionableObjects = new ICollisionable[0];
        }

        /// <summary>
        /// Redimensiona el vector de rectangulos de colision y añade un elemento.
        /// </summary>
        /// <param name="collisionRectangle"></param>
        public void AddRectangle(Rectangle collisionRectangle)
        {
            
            int newPositionInArray = _collisionBoxes.Length + 1;
            Array.Resize(ref _collisionBoxes, newPositionInArray);
            _collisionBoxes[newPositionInArray - 1] = collisionRectangle;
        }

        /// <summary>
        /// Redimensiona el vector de objetos ICollisionable y añade un elemento con colision activa.
        /// </summary>
        /// <param name="collisionRectangle"></param>
        public void AddICollisionable(ICollisionable collisionableElement)
        {

            int newPositionInArray = _collisionableObjects.Length + 1;
            Array.Resize(ref _collisionableObjects, newPositionInArray);
            _collisionableObjects[newPositionInArray - 1] = collisionableElement;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>
        /// True: Si hay colisión.
        /// False: Si NO hay colisión.
        /// </returns>
        public void checkCollisions()
        {
            for (int i = 0; i < _collisionableObjects.Length; i++)
            {
                for (int j = 0; j < _collisionBoxes.Length; j++)
                {                    
                    while (_collisionBoxes[j].Intersects(_collisionableObjects[i].GetBoundingBox()))
                    {
                        _collisionableObjects[i].AtferCollisionAction();
                    }
                }
            }
        }

    }
}
